package com.aiyige.utils.widget.articleview.adapter;

import android.os.Build;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.publish.util.PublishUtil;
import com.aiyige.utils.InputNumFilter;
import com.aiyige.utils.KeyboardUtil;
import com.aiyige.utils.widget.ZanyEditText;
import com.aiyige.utils.widget.articleview.event.EventArticleTextNodeFocusChanged;
import com.aiyige.utils.widget.articleview.event.EventArticleTextNodeTextChanged;
import com.aiyige.utils.widget.articleview.model.ArticleNode;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import net.cachapa.expandablelayout.ExpandableLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zhuyakun on 2018/3/13.
 */

public class ArticlePreviewAdapter extends BaseQuickAdapter<ArticleNode, BaseViewHolder> {
    public ArticlePreviewAdapter() {
        super(new LinkedList<ArticleNode>());
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ArticleNode.NODE_TYPE_PHOTO:
                return new PhotoNodeViewHolder(getItemView(R.layout.article_preview_photo_node, parent));
            case ArticleNode.NODE_TYPE_TEXT:
                return new TextNodeViewHolder(getItemView(R.layout.article_preview_text_node, parent));
            case ArticleNode.NODE_TYPE_VIDEO:
                return new VideoNodeViewHolder(getItemView(R.layout.article_preview_video_node, parent));
            default:
                return new BaseViewHolder(getItemView(R.layout.wild_card_item, parent));
        }
    }

    @Override
    protected int getDefItemViewType(int position) {
        return getItem(position).getType();
    }

    @Override
    protected void convert(BaseViewHolder helper, ArticleNode item) {
        switch (item.getType()) {
            case ArticleNode.NODE_TYPE_PHOTO:
                ((PhotoNodeViewHolder) helper).bindData(item);
                break;
            case ArticleNode.NODE_TYPE_TEXT:
                ((TextNodeViewHolder) helper).bindData(item);
                break;
            case ArticleNode.NODE_TYPE_VIDEO:
                ((VideoNodeViewHolder) helper).bindData(item);
                break;
        }
    }

    public class TextNodeViewHolder extends BaseViewHolder {
        @BindView(R.id.textNodeTv)
        TextView textNodeTv;

        public TextNodeViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(ArticleNode data) {
            textNodeTv.setText(data.getText());
            int textStyle;
            switch (data.getTextStyle()) {
                case ArticleNode.TEXT_STYLE_BIG_TITLE:
                    textStyle = R.style.TextNodeBigTitleStyle;
                    break;
                case ArticleNode.TEXT_STYLE_MAIN_BODY:
                    textStyle = R.style.TextNodeMainBodyStyle;
                    break;
                case ArticleNode.TEXT_STYLE_SMALL_TITLE:
                    textStyle = R.style.TextNodeSmallTitleStyle;
                    break;
                default:
                    textStyle = R.style.TextNodeMainBodyStyle;
                    break;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                textNodeTv.setTextAppearance(textStyle);
            } else {
                textNodeTv.setTextAppearance(itemView.getContext(), textStyle);
            }
        }
    }

    public class VideoNodeViewHolder extends BaseViewHolder {
        @BindView(R.id.coverIv)
        ImageView coverIv;

        public VideoNodeViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(ArticleNode data) {
            Glide.with(itemView.getContext())
                    .load(data.getCoverUrl())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(coverIv);
        }
    }

    public class PhotoNodeViewHolder extends BaseViewHolder {
        @BindView(R.id.photoIv)
        ImageView photoIv;
        @BindView(R.id.introductionTv)
        TextView introductionTv;

        public PhotoNodeViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(ArticleNode data) {
            Glide.with(itemView.getContext())
                    .asBitmap()
                    .load(data.getUrl())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(photoIv);
            if (!TextUtils.isEmpty(data.getText())) {
                introductionTv.setVisibility(View.VISIBLE);
                introductionTv.setText(data.getText());
            } else {
                introductionTv.setVisibility(View.GONE);
            }
        }
    }
}
